package cn.rongdao.jrkabao.test

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import cn.rongdao.jrkabao.R
import cn.rongdao.jrkabao.R.id.atRecyclerView
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity
import cn.rongdao.jrkabao.bean.main.personal.PersonItemInfoBean
import cn.rongdao.jrkabao.model.main.personal.PersonItemInfoModel
import com.mifengkong.frtools.http.request.callback.FRBaseObserver
import com.mifengkong.frtools.ui.activity.FRStartActivity
import com.mifengkong.kotlincommon.recyclerview.adapter.OnRecyclerItemClickListener
import com.mifengkong.kotlincommon.recyclerview.refreshload.refresh.DefaultRefreshView
import com.mifengkong.kotlincommon.recyclerview.refreshload.refresh.DropDownRefreshRecyclerView
import kotlinx.android.synthetic.main.activity_test.*
import java.util.*

/**
 * Created by jack on 17/5/9
 */

class TestActivity : BaseAutoFinishActivity() {

    companion object {
        @JvmStatic
        fun start(context: Context) {
            FRStartActivity.start(context, TestActivity::class.java)
        }
    }

    private lateinit var personalItemInfoAdapter: TestPersonAdapter

    override fun layoutRes(): Int {
        return R.layout.activity_test
    }

    override fun initView() {
        super.initView()
        personalItemInfoAdapter = TestPersonAdapter(thisActivity())

        atRecyclerView.layoutManager = LinearLayoutManager(thisActivity())

        atRecyclerView.addRefreshViewCreator(DefaultRefreshView())

        atRecyclerView.adapter = personalItemInfoAdapter

        atRecyclerView.setOnRefreshListener(object : DropDownRefreshRecyclerView.OnRefreshListener {
            override fun onRefresh() {
                getPersonData()
            }

        })

        personalItemInfoAdapter.setOnItemClickListener(object : OnRecyclerItemClickListener.OnItemClickListener {
            override fun click(view: View, position: Int) {
                personalItemInfoAdapter.removeItem(position)
            }
        })
    }

    private fun getPersonData() {
        PersonItemInfoModel.getInstance().execute(object : FRBaseObserver<ArrayList<PersonItemInfoBean>>(this) {
            override fun onBaseNext(data: ArrayList<PersonItemInfoBean>) {
                if (data.isNotEmpty()) {
                    val personItemInfoBean = data[data.size - 1]
                    personItemInfoBean.isIs_last_item = true
                    data[data.size - 1] = personItemInfoBean
                    personalItemInfoAdapter.setDataList(data)
                }
                atRecyclerView.onRefreshComplete()
            }

            override fun onBaseError(t: Throwable) {
                super.onBaseError(t)
                atRecyclerView.onRefreshComplete()
            }
        })
    }
}